﻿/* 
 * 
 * Copyright 2013 Bendsoft. All rights reserved.
 * http://www.bendsoft.com
 * 
 * This code is distributed under the New BSD License (BSD).
 * 
 * @package     Camelot SharePoint Integration Library
 * @codeplex    http://camelottoolkit.codeplex.com
 * @license     New BSD License (BSD)
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Camelot.SharePointIntegration.Client
{
    public class DocumentLibraryData
    {
        public ListDescription Description { get; protected internal set; }
        public DocumentLibraryNodeCollection Documents { get; protected internal set; }

        protected internal static DocumentLibraryNode CreateDocumentLibraryNode(DataRow row)
        {
            var documentUrl = ((string)row["ServerUrl"]).Split('/');
            return new DocumentLibraryNode()
            {
                Id = (int)row["ID"],
                Guid = (Guid)row["GUID"],
                Name = documentUrl.Last(),
                Url = (string)row["ServerUrl"],
                ParentUrl = string.Join("/", documentUrl, 0, documentUrl.Length - 1),
                ContentType = (string)row["ContentType"],
                FileType = Convert.IsDBNull(row["File Type"]) ? null : (string)row["File Type"],
                FileSize = Convert.IsDBNull(row["FileSizeDisplay"]) ? 0 : Convert.ToInt32(row["FileSizeDisplay"]),
                Created = (DateTime)row["Created"],
                CreatedBy = Convert.IsDBNull(row["Created By"]) ? null : (string)row["Created By"],
                Modified = (DateTime)row["Modified"],
                ModifiedBy = Convert.IsDBNull(row["Modified By"]) ? null : (string)row["Modified By"],
                CheckedOutBy = Convert.IsDBNull(row["CheckoutUser"]) ? null : (string)row["CheckoutUser"],
                ProcedureUrl = documentUrl[0] == "" ? string.Join("/", documentUrl, 2, documentUrl.Length - 2) : (string)row["ServerUrl"],
                Level = documentUrl[0] == "" ? (documentUrl.Length - 3) : (documentUrl.Length - 1)
            };
        }

        public static DocumentLibraryData CreateDocumentLibraryData(ListData listData)
        {
            var data = new DocumentLibraryData()
            {
                Description = listData.Description
            };

            if (data.Description != null && data.Description.BaseType != ListType.DocumentLibrary)
                throw new Exception("Expected list of type document library");

            var documents = new DocumentLibraryNodeCollection();

            foreach (var doc in listData.Rows.AsEnumerable().Where(n1 => (string)n1["ContentType"] == "Folder").OrderBy(n2 => n2["BaseName"]))
            {
                documents.Add(DocumentLibraryData.CreateDocumentLibraryNode(doc));
            }

            foreach (var doc in listData.Rows.AsEnumerable().Where(n1 => (string)n1["ContentType"] != "Folder").OrderBy(n2 => n2["BaseName"]))
            {
                documents.Add(DocumentLibraryData.CreateDocumentLibraryNode(doc));
            }

            data.Documents = documents;
            return data;
        }
    }
}